package com.tssm.core.util;

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;

/**
 * 
 * @类介绍:字符串转换操作
 * @类名称:Str
 * @作者:徐龙
 * @日期:2010-5-23
 */
public class Str {

	public Str() {
		super();
	}

	/**
	 * 
	 * 
	 * @方法说明: 从Decimal 转换为 int
	 * @param bd
	 *            Decimal
	 * @return
	 * @作者:徐龙
	 * @日期:2010-5-23
	 */
	public int decimalToInt(BigDecimal bd) {
		return bd.intValue();
	}

	/**
	 * 
	 * 
	 * @方法说明:把传入的编码类型字符串转成ISO-8859-1类型字符串然后通过URLEncoder转码
	 * @param str
	 *            字符串
	 * @param codeName
	 *            编码类型 如 UTF-8,GBK等
	 * @return
	 * @作者:徐龙
	 * @日期:2010-5-23
	 */

	public static String enStrCode(String str, String codeName) {
		String newstr = null;
		try {
			newstr = new String(URLEncoder.encode(str, codeName).getBytes(
					codeName), "ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newstr;
	}

	/**
	 * 
	 * 
	 * @方法说明:把ISO-8859-1类型字符串转成传入的编码类型字符串然后通过URLDecoder解码
	 * @param str
	 *            字符串
	 * @param codeName
	 *            编码类型 如 UTF-8,GBK等
	 * @return
	 * @作者:徐龙
	 * @日期:2010-5-23
	 */
	public static String deStrCode(String str, String codeName) {
		String newstr = null;
		try {
			newstr = new String(URLDecoder.decode(str, codeName).getBytes(
					"ISO-8859-1"), codeName);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newstr;
	}

	/**
	 * 
	 * 
	 * @方法说明: 返回中文字符串
	 * @param str
	 * @return
	 * @作者:徐龙
	 * @日期:2010-5-23
	 */
	public static String chStr(String str) {
		if (str == null) {
			str = "";
		} else {
			try {
				str = (new String(str.getBytes("iso-8859-1"), "GBK")).trim();
			} catch (Exception e) {
				System.out.println("chStr");
				System.out.println(e.getMessage());
			}
		}
		return str;
	}
	/**
	 * 
	 * 
	 * @方法说明: 返回中文字符串
	 * @param str
	 * @return
	 * @作者:徐龙
	 * @日期:2010-5-23
	 */
	public static String chUTFStr(String str) {
		if (str == null) {
			str = "";
		} else {
			try {
				str = (new String(str.getBytes("iso-8859-1"), "utf-8")).trim();
			} catch (Exception e) {
				System.out.println("chStr");
				System.out.println(e.getMessage());
			}
		}
		return str;
	}

	/**
	 * 
	 * 
	 * @方法说明: 返回unicode字符串
	 * @param str
	 * @return
	 * @作者:徐龙
	 * @日期:2010-5-23
	 */
	public static String unicodeStr(String str) {
		if (str == null) {
			str = "";
		} else {
			try {
				str = (new String(str.getBytes("GBK"), "iso-8859-1")).trim();
			} catch (Exception e) {
				System.out.println("chStr");
				System.out.println(e.getMessage());
			}
		}
		return str;
	}

	/**
	 * 
	 * 
	 * @方法说明: 从HTML代码转到Text代码
	 * @param s
	 * @return
	 * @作者:徐龙
	 * @日期:2010-5-23
	 */
	public static String toHtmlText(String s) {
		if (s == null)
			return s;
		s = strReplace(s, "&", "＆");
		s = strReplace(s, "\"", "&quot;");
		s = strReplace(s, "(", "（");
		s = strReplace(s, ")", "）");
		s = strReplace(s, "<", "&lt;");
		s = strReplace(s, ">", "&gt;");
		s = strReplace(s, "*", "×");
		s = strReplace(s, "?", "？");
		s = strReplace(s, "'", "‘");
		s = strReplace(s, ".", "。");
		s = strReplace(s, ";", "；");
		s = strReplace(s, "!", "！");
		s = strReplace(s, "&nbsp;", "　");

		return s;
	}

	/**
	 * 
	 * 
	 * @方法说明:从Text代码转到HTML代码
	 * @param s
	 * @return
	 * @作者:徐龙
	 * @日期:2010-5-23
	 */
	public static String toTextHtml(String s) {
		if (s == null)
			return s;
		s = strReplace(s, "；", ";");
		s = strReplace(s, "＆", "&");
		s = strReplace(s, "（", "(");
		s = strReplace(s, "）", ")");
		s = strReplace(s, "&lt;", "<");
		s = strReplace(s, "&gt;", ">");
		s = strReplace(s, "&quot;", "\"");
		s = strReplace(s, "＂", "\"");
		s = strReplace(s, "×", "*");
		s = strReplace(s, "？", "?");
		s = strReplace(s, "‘", "'");
		s = strReplace(s, "。", ".");
		s = strReplace(s, "！", "!");
		s = strReplace(s, "&nbsp;&nbsp;", "　");
		s = strReplace(s, "IMG　　", "IMG ");
		s = strReplace(s, "img　　", "img ");
		return s;
	}

	/**
	 * 
	 * 
	 * @方法说明: 替换字符串
	 * @param sBody
	 *            字符串
	 * @param sFrom
	 *            旧字符串
	 * @param sTo
	 *            新字符串
	 * @return
	 * @作者:徐龙
	 * @日期:2010-5-23
	 */
	public static String strReplace(String sBody, String sFrom, String sTo) {
		int i = 0;
		int j = sFrom.length();
		int k = sTo.length();
		if (k < j) {
			k = sBody.length();
		} else {
			k = sBody.length() * 2;
		}
		StringBuffer ret = new StringBuffer(k);
		while (sBody.indexOf(sFrom, i) != -1) {
			ret.append(sBody.substring(i, sBody.indexOf(sFrom, i)) + sTo);
			i = sBody.indexOf(sFrom, i);
			i += j;
		}
		ret.append(sBody.substring(i));
		String rd = ret.toString();
		ret = null;
		return rd;

	}

	/**
	 * 
	 * 
	 * @方法说明: 过滤图片（即图片不输出)
	 * @param str
	 * @return
	 * @作者:徐龙
	 * @日期:2010-5-23
	 */
	public static String imgFilter(String str) {
		String newS = "";
		String s = "<(img|IMG).[^>]*src(=|   )(.[^>]*)>";
		Pattern p = Pattern.compile(s);
		String[] newStr = p.split(str);
		for (int i = 0; i < newStr.length; i++) {
			newS = newS + newStr[i];
		}
		return newS;
	}

	/**
	 * 
	 * 
	 * @方法说明: 过滤HTML代码
	 * @param str
	 * @return
	 * @作者:徐龙
	 * @日期:2010-5-23
	 */
	public static String htmlFilter(String str) {
		return str.replaceAll("<.*?>", "");
	}

	/**
	 * 
	 * 
	 * @方法说明: 截取字符长度为len ,从start开始
	 * @param str
	 * @param start
	 *            开始
	 * @param len
	 *            长度
	 * @return
	 * @作者:徐龙
	 * @日期:2010-5-23
	 */
	public static String strSubString(String str, int start, int len) {
		if (str.length() >= len) {
			str = str.substring(start, len);
		}
		String newstr = str;
		return newstr;
	}

	/**
	 * 
	 * 
	 * @方法说明: 通过文件地址得到相关文件对象
	 * @param filename
	 *            文件地址
	 * @return
	 * @作者:徐龙
	 * @日期:2010-5-23
	 */
	public static Image getImage(String filename) {
		File filebiao = new File(filename);
		Image srcbiao = null;
		try {
			if (filebiao.exists()) {
				srcbiao = ImageIO.read(filebiao);
			}
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return srcbiao;
	}

}
